﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using UserIdentityDemo.Models;

namespace UserIdentityDemo.Services
{
    public static class UserAuthorization
    {
        public static ClaimsPrincipal GetClaimsPrincipal(User user)
        {
            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, user.UserName, ClaimValueTypes.String, Constants.Issuer),
                new Claim(Constants.CompanyClaimType, user.Company, ClaimValueTypes.String, Constants.Issuer)
            };
            if (!string.IsNullOrWhiteSpace(user.Role))
            {
                claims.Add(new Claim(ClaimTypes.Role, user.Role, ClaimValueTypes.String, Constants.Issuer));
            }

            var userIdentity = new ClaimsIdentity("UserIdentity");
            userIdentity.AddClaims(claims);

            var userPrincipal = new ClaimsPrincipal(userIdentity);

            return userPrincipal;
        }
    }
}